package com.kandinfo.isbnmanage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;

import com.kandinfo.isbnmanage.domian.IsbnManage;
import com.kandinfo.isbnmanage.dto.IsbnDigDto;
import com.kandinfo.isbnmanage.enums.IsbnRoleCodeEnum;
import com.kandinfo.isbnmanage.repository.IsbnManageRepository;
import com.kandinfo.isbnmanage.repository.impl.IsbnDigSearchRepository;
import com.kandinfo.isbnmanage.service.IsbnAutoDistributeService;
import com.kandinfo.isbnmanage.service.IsbnManageService;
import com.kandinfo.isbnmanage.service.IsbnQuartzService;

@Service
public class IsbnQuartzServiceImpl implements IsbnQuartzService{
	
	private static final Logger LOG = LoggerFactory.getLogger(IsbnQuartzServiceImpl.class);
	
	@Autowired
	IsbnDigSearchRepository isbnDigSearchRepository;
	
	@Autowired
	IsbnManageService isbnManageService;
	
	@Autowired
	IsbnAutoDistributeService isbnAutoDistributeService;
	
	@Autowired
	IsbnManageRepository isbnManageRepository;
	
	@Override
	@Transactional()
	public void dig() {
		List<IsbnDigDto> isbnDigDtoList = isbnDigSearchRepository.getIsbnDigDtoList();
		if (CollectionUtils.isEmpty(isbnDigDtoList)) {
			return;
		}
		List<IsbnDigDto> rodoList  = removeLocal(isbnDigDtoList);
		if (CollectionUtils.isEmpty(rodoList)) {
			return;
		}
		List<IsbnManage> list = new ArrayList<IsbnManage>();
		int perCount = 1000;
		Date createTs = new Date();
		for (IsbnDigDto isbnDigDto : rodoList) {
			IsbnManage isbnManage = new IsbnManage();
			isbnManage.setApplicationCount(Long.valueOf(isbnDigDto.getUserCount()));
			isbnManage.setBarcode(isbnDigDto.getBarcode());
			String provinces = isbnDigDto.getProvinces();
			if (StringUtils.isNotBlank(provinces)) {
				if (",".equals(provinces.substring(0, 1))) {
					provinces = provinces.substring(1, provinces.length());
				}
			}
			isbnManage.setProvince(provinces);
			isbnManage.setCreateTs(createTs);
			isbnManage.setCommitTs(isbnDigDto.getFirstUploadTime());
			isbnManage.setCreateBy(1L);
			isbnManage.setOrderCount(0);
			isbnManage.setPublishCount(0);
			isbnManage.setReceiptCount(0);
			isbnManage.setScanCount(0);
			isbnManage.setStatus(0);
			isbnManage.setUploadCount(0);
			isbnManage.setIsbnInformationCount(0);
			list.add(isbnManage);
			if (list.size() == perCount) {
				isbnManageService.save(list);
				if (!autoDistributePurChase(list)) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					LOG.error("DIG autoDistributePurChase ERROR ");
					return;
				}
				list.clear();
			}
		}
		if (!CollectionUtils.isEmpty(list)) {
			isbnManageService.save(list);
			if (!autoDistributePurChase(list)) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				LOG.error("DIG autoDistributePurChase ERROR ");
				return;
			}
		}
		
	}
	
	/**
	 * 去除本地已有的条形码
	 * @param isbnDigDtoList
	 */
	private List<IsbnDigDto> removeLocal(List<IsbnDigDto> isbnDigDtoList) {
		List<IsbnDigDto> result = new ArrayList<IsbnDigDto>();
		List<String> barcodeList = new ArrayList<String>();
		Map<String, IsbnDigDto> map = new HashMap<String, IsbnDigDto>();
		for (IsbnDigDto isbnDigDto : isbnDigDtoList) {
			barcodeList.add(isbnDigDto.getBarcode());
			map.put(isbnDigDto.getBarcode(), isbnDigDto);
		}
		List<IsbnManage> localList = isbnManageRepository.findByBarcode(barcodeList);
		if (CollectionUtils.isEmpty(localList)) {
			result.addAll(isbnDigDtoList);
			return result;
		}
		for (IsbnManage isbnManage : localList) {
			String barcode = isbnManage.getBarcode();
			map.remove(barcode);
		}
		for (IsbnDigDto isbnDigDto : map.values()) {
			result.add(isbnDigDto);
		}
		return result;
	} 

	/**
	 * 自动分发购书
	 * 
	 * @param list
	 */
	private boolean autoDistributePurChase(List<IsbnManage> list ){
		List<Long> refKeyList = new ArrayList<Long>();
		for (IsbnManage isbnManage : list) {
			refKeyList.add(isbnManage.getId());
		}
		return isbnAutoDistributeService.autoDistributeMuti(IsbnRoleCodeEnum.ISBN_PERCHANCE, refKeyList);
	}
	
}
